﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.OA.Email
{
    public partial class email_manages_list : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                #region  根据类别不同隐藏不同列

                ViewState["TITLE"] = (null != Request.QueryString["TITLE"]) ? Request.QueryString["TITLE"].ToString() : "";
                ViewState["YJBZ"] = (null != Request.QueryString["YJBZ"]) ? Request.QueryString["YJBZ"].ToString() : "0";
                ViewState["YJJXH"] = (null != Request.QueryString["YJJXH"]) ? Request.QueryString["YJJXH"].ToString() : "";


                string sState = ViewState["YJBZ"].ToString().Trim();
                string sTitle = ViewState["TITLE"].ToString().Trim();
                string sYJJXH = ViewState["YJJXH"].ToString().Trim();
                string sparam = "TITLE=" + sTitle + "&YJBZ=" + sState + "&YJJXH=" + sYJJXH;
                hidTitle.Value = sTitle;
                hidParams.Value = Base64Lib.Encode(sparam);

                //0收件箱   2已删除 3草稿箱   1已发送
                if (sState == "3" || sState == "1")
                {
                    grid.Columns[5].Visible = false;
                }
                else
                {
                    grid.Columns[6].Visible = false;
                }
                #endregion

                
            }
            BindGridView();
        }

        private void BindGridView()
        {
            grid.DataSource = GetDateBind();
            grid.DataBind();
        }

        private DataSet GetDateBind()
        {
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            string UserId = loginUser.UserId;
            string sState = ViewState["YJBZ"].ToString().Trim();
            string sYJJXH = ViewState["YJJXH"].ToString().Trim();
            EmailBLL bll = new EmailBLL();
            MsEmailRecord msRecord = new MsEmailRecord();
            msRecord.statelogo = sState;
            msRecord.userId = UserId;
            hidUserId.Value = UserId;
            if (sYJJXH == "")
            {
                sYJJXH = "0";
            }
            msRecord.folderId = sYJJXH;
            DataSet dSet = bll.GetEmailTypeList(msRecord);
            bll.Release();
            bll = null;
            return dSet;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DelEmail(string serExpr, string operType)
        {
            bool bFlag = false;
            MsEmailRecord msRecord = (MsEmailRecord)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsEmailRecord));
            EmailBLL bll = new EmailBLL();
            bFlag = bll.DelEmail(msRecord);

            string sResult = (bFlag) ? "1" : "0";
            var newObj = new { operType = operType, resultSign = sResult };
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            newObj = null;
            msRecord = null;
            return resultSerExpr;
        }

        /// <summary>
        /// 转移邮件
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string FindEmail(string serExpr, string operType)
        {
            bool bFlag = false;
            MsEmailRecord msRecord = (MsEmailRecord)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsEmailRecord));
            EmailBLL bll = new EmailBLL();
            bFlag = bll.FindEmail(msRecord);

            string sResult = (bFlag) ? "1" : "0";
            var newObj = new { operType = operType, resultSign = sResult };
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            newObj = null;
            msRecord = null;
            return resultSerExpr;
        }


        protected void grid_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            string sType = ViewState["YJBZ"].ToString().Trim();
            string sYJJXH = ViewState["YJJXH"].ToString().Trim();
            string sXH = grid.GetRowValues(e.VisibleIndex, "YJID").ToString().Trim();
            string sYJJL = grid.GetRowValues(e.VisibleIndex, "YJJL").ToString().Trim();
            string sTitle = grid.GetRowValues(e.VisibleIndex, "YJBT").ToString().Trim();
            string sLength = grid.GetRowValues(e.VisibleIndex, "YJDX").ToString().Trim();
            string sSFYD = grid.GetRowValues(e.VisibleIndex, "SFYD").ToString().Trim();
            string sJSSJ = grid.GetRowValues(e.VisibleIndex, "JSSJ").ToString().Trim();
            string sparam = "YJJXH=" + sYJJXH + "&TITLE=" + ViewState["TITLE"].ToString().Trim() + "&YJBZ=" + sType + "&YJID=" + sXH + "&CKLX=1&YJJL=" + sYJJL + "&SFYD=" + sSFYD;
            //string encodeparam = Base64Lib.Encode(sparam);
            //阅读标志 0 未阅读 1 已阅读
            string sState = grid.GetRowValues(e.VisibleIndex, "SFYD").ToString().Trim();
            if (sType == "0" && sState == "1")
            {
                e.Row.Cells[2].Text = "<img title='已阅读' src='../images/toolbar/Read.gif' border=0>";
            }
            else if (sState == "0")
            {
                e.Row.Cells[2].Text = "<img title='未阅读' src='../images/toolbar/UnRead.gif' border=0>";
            }
            else
            {
                e.Row.Cells[2].Text = "<img  title='已阅读' src='../images/toolbar/Read.gif' border=0>";
            }
            e.Row.Cells[3].Text = "<img title='附件数0' src='../images/toolbar/Attach.gif' border=0>";
            if (sType != "3") //非 草稿箱
            {
                e.Row.Cells[5].Text = "<a href=\"javascript:doView('','" + sparam + "')\">" + sTitle + "</a>";
            }
            else
            {
                e.Row.Cells[5].Text = "<a href=\"javascript:doView('3','" + sparam + "')\">" + sTitle + "</a>";
            }
            if (sLength != "")
            {
                e.Row.Cells[7].Text = double.Parse(sLength).ToString("F2") + "(K)";
            }
            else {
                e.Row.Cells[7].Text = "0.00(K)";
            }
            #region 当为草稿箱时
            if (sType == "3")
            {
                string sSJRBZ = grid.GetRowValues(e.VisibleIndex, "SJRBZ").ToString().Trim();  //收件人标志
                string sSJRFW = grid.GetRowValues(e.VisibleIndex, "SJR").ToString().Trim();    //收件人范围

                e.Row.Cells[4].Text = Ccit.OA.Utils.SysInterface.GetNameStrByIdStr(sSJRFW, sSJRBZ, " ");
            }
            #endregion
        }
    }
}
